package org.ucn.dmaa0914.gruppe6.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.ArrayList;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import org.ucn.dmaa0914.gruppe6.CrudController;
import org.ucn.dmaa0914.gruppe6.IdObject;

import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.Box;

/**
 * @author Kim Johannsen
 * @version 02-02-2015 
 */
public class SearchView<T> extends JPanel {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtSearchText;
	private JList<T> resultList;
	private DefaultListModel<T> resultListModel;
	
	CrudController<T> controller;
	private ArrayList<T> searchResults;

	/**
	 * Create the dialog.
	 */
	public SearchView(CrudController<T> controller) {
		this.controller = controller;
		this.resultListModel = new DefaultListModel<T>();
		setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		add(contentPanel, BorderLayout.CENTER);
		{
			resultList = new JList<T>();
			resultList.setModel(resultListModel);
			resultList.setCellRenderer(new ListCellRenderer<T>() {

				protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();

				@Override
				public Component getListCellRendererComponent(
						JList<? extends T> list, T value, int index,
						boolean isSelected, boolean cellHasFocus)
				{
					JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index,
							isSelected, cellHasFocus);
					renderer.setText(controller.format(((IdObject)value).getId()));
					return renderer;
				}


			});
			resultList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			
			contentPanel.setLayout(new FormLayout(new ColumnSpec[] {
					FormSpecs.UNRELATED_GAP_COLSPEC,
					ColumnSpec.decode("pref:grow"),
					FormSpecs.UNRELATED_GAP_COLSPEC,},
				new RowSpec[] {
					FormSpecs.RELATED_GAP_ROWSPEC,
					FormSpecs.DEFAULT_ROWSPEC,
					FormSpecs.UNRELATED_GAP_ROWSPEC,
					RowSpec.decode("26px"),
					FormSpecs.LABEL_COMPONENT_GAP_ROWSPEC,
					RowSpec.decode("pref:grow"),}));
			{
				Component horizontalStrut = Box.createHorizontalStrut(200);
				contentPanel.add(horizontalStrut, "2, 2");
			}
			
			contentPanel.add(new JScrollPane(resultList), "2, 6, fill, fill");
		}

		JPanel panel = new JPanel();
		contentPanel.add(panel, "2, 4, fill, top");
		panel.setLayout(new FormLayout(new ColumnSpec[] {
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("pref:grow"),},
				new RowSpec[] {
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,}));
		{
			JLabel lblSearchText = new JLabel("Filter:");
			panel.add(lblSearchText, "2, 2");
		}
		{
			txtSearchText = new JTextField();
			panel.add(txtSearchText, "4, 2");

			// Make sure we update search results every time text is changed
			txtSearchText.getDocument().addDocumentListener(new DocumentListener() {

				@Override
				public void removeUpdate(DocumentEvent e) {
					updateSearchResults();
				}

				@Override
				public void insertUpdate(DocumentEvent e) {
					updateSearchResults();
				}

				@Override
				public void changedUpdate(DocumentEvent e) {
					updateSearchResults();
				}
			});
		}
		
		
		updateSearchResults();
		setVisible(true);
	}
	
	public void updateSearchResults()
	{
		T selected = resultList.getSelectedValue();
		searchResults = controller.search(txtSearchText.getText());
		resultListModel.clear();
		for(T p : searchResults)
		{
			resultListModel.addElement(p);
		}
		resultList.setSelectedValue(selected, true);
	}

	public JList<T> getList()
	{
		return resultList;
	}
}
